﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Markup;
using Core = Vision.Core;

namespace Vision.Core.ActivityComponents.IO.File
{
    /// <summary>
    /// 
    /// </summary>
	[ContentProperty("Body")]
	[System.ComponentModel.Designer(typeof(FileSaveDesigner))]
    public class FileSave : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        [DefaultValue(null)]
        [Category("Input")]
        public InArgument<string> FilePath { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DefaultValue(null)]
        [Category("Input")]
        public InArgument<string> FileName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DefaultValue(null)]
        [Category("Input")]
        public InArgument<byte[]> FileData { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[DefaultValue(null)]
		[Category("Output")]
		public OutArgument<string> FullFileName { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[DefaultValue(null)]
		//[DependsOn("FullFileName")]
		public ActivityAction<string> Body { get; set; }

		public FileSave()
			: base()
		{
			this.Body = new ActivityAction<string>()
			{
				Argument = new DelegateInArgument<string>()
				{
					Name = "fullFileName"
				}
			};
		}

		protected override void CacheMetadata(NativeActivityMetadata metadata)
		{
			RuntimeArgument filePathArgument = new RuntimeArgument("FilePath", typeof(string), ArgumentDirection.In, true);
			metadata.Bind(this.FilePath, filePathArgument);
			metadata.AddArgument(filePathArgument);

			RuntimeArgument fileNameArgument = new RuntimeArgument("FileName", typeof(string), ArgumentDirection.In, true);
			metadata.Bind(this.FileName, fileNameArgument);
			metadata.AddArgument(fileNameArgument);

			RuntimeArgument fileDataArgument = new RuntimeArgument("FileData", typeof(byte[]), ArgumentDirection.In, true);
			metadata.Bind(this.FileData, fileDataArgument);
			metadata.AddArgument(fileDataArgument);

			RuntimeArgument fullFileArgument = new RuntimeArgument("FullFileName", typeof(string), ArgumentDirection.Out, false);
			metadata.Bind(this.FullFileName, fullFileArgument);
			metadata.AddArgument(fullFileArgument);

			metadata.AddDelegate(this.Body);
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            string filePath = context.GetValue(this.FilePath) + "\\" + context.GetValue(this.FileName);
            byte[] fileData = context.GetValue(this.FileData);

            using (FileStream fStream = System.IO.File.Create(filePath)){
                fStream.Write(fileData, 0, fileData.Length);
                fStream.Flush();
            }

			// set the output path data
			context.SetValue(this.FullFileName, filePath);

			// schedule the next action
			if (this.Body != null && this.Body.Handler != null)
				context.ScheduleAction(this.Body, filePath);
        }
    }
}
